package com.xh.rufongwallpaper.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xh.rufongwallpaper.common.constant.Constants;
import com.xh.rufongwallpaper.common.result.Result;
import com.xh.rufongwallpaper.common.util.VOConverter;
import com.xh.rufongwallpaper.entity.DownloadRecord;
import com.xh.rufongwallpaper.service.DownloadService;
import com.xh.rufongwallpaper.service.WallpaperService;
import com.xh.rufongwallpaper.vo.DownloadRecordVO;
import com.xh.rufongwallpaper.vo.PageResponseVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 下载控制器
 *
 * @author XH
 */
@RestController
@RequestMapping("/downloads")
@RequiredArgsConstructor
@Tag(name = "下载API", description = "壁纸下载相关接口")
public class DownloadController {

    private final DownloadService downloadService;
    private final WallpaperService wallpaperService;


    @GetMapping("/device")
    @Operation(summary = "获取设备下载记录", description = "获取指定设备的壁纸下载记录")
    public Result<PageResponseVO<DownloadRecordVO>> getDeviceDownloads(
            @Parameter(description = "设备ID") @RequestHeader(value = "X-Device-ID") String deviceId,
            @Parameter(description = "页码，从1开始") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页记录数") @RequestParam(defaultValue = "20") int limit) {
        
        // 将页码转为0-based索引
        int pageIndex = Math.max(0, page - 1);
        int pageSize = Math.min(limit, Constants.MAX_PAGE_SIZE);
        
        Page<DownloadRecord> records = downloadService.getDownloadsByDeviceId(deviceId, pageIndex, pageSize);
        return Result.success(VOConverter.convertToDownloadRecordPageResponse(records));
    }

    @GetMapping("/wallpaper/{wallpaperId}")
    @Operation(summary = "获取壁纸下载记录", description = "获取指定壁纸的下载记录")
    public Result<PageResponseVO<DownloadRecordVO>> getWallpaperDownloads(
            @Parameter(description = "壁纸ID") @PathVariable Long wallpaperId,
            @Parameter(description = "页码，从1开始") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页记录数") @RequestParam(defaultValue = "20") int limit) {
        
        // 确保壁纸存在
        if (!wallpaperService.existsById(wallpaperId)) {
            return Result.failure("壁纸不存在");
        }
        
        // 将页码转为0-based索引
        int pageIndex = Math.max(0, page - 1);
        int pageSize = Math.min(limit, Constants.MAX_PAGE_SIZE);
        
        Page<DownloadRecord> records = downloadService.getDownloadsByWallpaperId(wallpaperId, pageIndex, pageSize);
        return Result.success(VOConverter.convertToDownloadRecordPageResponse(records));
    }
    
    @GetMapping("/count")
    @Operation(summary = "获取下载数统计", description = "获取设备的下载总数")
    public Result<Long> getDownloadCount(
            @Parameter(description = "设备ID") @RequestHeader(value = "X-Device-ID") String deviceId) {
        
        long count = downloadService.countDownloadsByDeviceId(deviceId);
        return Result.success(count);
    }
    
    @GetMapping("/check/{wallpaperId}")
    @Operation(summary = "检查是否已下载", description = "检查设备是否已下载指定壁纸")
    public Result<Boolean> checkDownloaded(
            @Parameter(description = "设备ID") @RequestHeader(value = "X-Device-ID") String deviceId,
            @Parameter(description = "壁纸ID") @PathVariable Long wallpaperId) {
        
        boolean downloaded = downloadService.hasDownloaded(deviceId, wallpaperId);
        return Result.success(downloaded);
    }
} 